export type bannerPos = 'top' | 'bottom';

export class adClass {
    debugMode: boolean = true;//广告调试模式
    appId: string = "4B944975BECCFEA57B844F6C9DDB2AD7"//"a6098dcc2b65d5";  
    // appkey: string = "4f7b9ac17decb9babec83aac078742c7"//'f0b22d9a956154f3d71568d05d015fda';
    // placementID: string = "b6180faba26ca9"//"b60cd3c15b2923"; //视频广告位id
    // sceneVideoId: string = "f6180faf1a99f1";
    alreadyInit: boolean =  false; //是否初始化
    placementBannerID: string = "7FAA7FA1648F3853E06748B6B9689F8B";

    banner: tradplus.Banner = null;
    rewardedVideo: tradplus.RewardedVideo = null;
    bannerAdUnitId = this.isAndroid()
          ? 'A9769659B100F567ECC41F0A4CA16700'
          : '6008C47DF1201CC875F2044E88FCD287';
    VideoAdUnitId = this.isAndroid()
        ? '39DAC7EAC046676C5404004A311D1DB1'
        : '7FAA7FA1648F3853E06748B6B9689F8B';
    _init() {
        if(cc.sys.isNative){
            if(this.alreadyInit) return ; //初始化过的化直接返回
            tradplus.tradPlusService.setEnableLog(this.debugMode);
            // GDPR监听器需要在初始化SDK之前调用
            tradplus.privacy.setGDPRListener({
                onSuccess: () => {
                // 已知国家
                if (tradplus.privacy.isGDPRApplicable()) {
                    console.log('GDPR is applicable');
                    this.showGDPR();
                } else {
                    console.log('GDPR is not applicable');
                }
                },
                onFailed: () => {
                // 未知国家，让用户自己选择GDPR等级
                    console.log('Unknown country, let the user choose the GDPR level');
                    this.showGDPR();
                },
            });
           
            tradplus.tradPlusService.initSdk();
            // 启用测试模式，仅测试时使用，上线前请注释掉
            tradplus.tradPlusService.setNeedTestDevice(true);
            this.alreadyInit = true;
        }
    }
    showGDPR() {
        if (!tradplus.privacy.isFirstShowGDPR()) {
          tradplus.privacy.showUploadDataNotifyDialog();
        } else {
          console.log('User already choosen a GDPR level');
        }
      }
    isAndroid() {
       return cc.sys.platform === cc.sys.ANDROID;
    }
    loadBannerAd() {
        this.banner = tradplus.tradPlusService.getBanner(this.bannerAdUnitId);
        this.banner.setAdListener({
          onAdLoaded: (adSourceName) => {
            console.log('onAdLoaded, adSourceName:', adSourceName);
          },
    
          onAdClicked: () => {
            console.log('onAdClicked');
          },
    
          onAdLoadFailed: (adError) => {
           console.log('onAdLoadFailed, adError:', adError);
          },
    
          onAdImpression: () => {
            console.log('onAdImpression');
          },
    
          // Android only
          onAdShowFailed: (adError) => {
            console.log('onAdShowFailed, adError:', adError);
          },
    
          // Android only
          onAdClosed: () => {
            console.log('onAdClosed');
          },
    
          // Android only
          onBannerRefreshed: () => {
            console.log('onBannerRefreshed');
          },
        });
    
    }
    loadVideoAd() {
        this.rewardedVideo = tradplus.tradPlusService.getRewardedVideo(
            this.VideoAdUnitId,
            true
        );
        this.rewardedVideo.entryAdScenario('177010A4403105');
        const customMap = {
            user_gender: 'male',
            user_level: '10',
        };
        tradplus.tradPlusService.initPlacementCustomMap(this.VideoAdUnitId, customMap);
        this.rewardedVideo.setAdListener({
            onAdAllLoaded: (anyAdsLoadSucceeded) => {
              console.log(
                'onAdAllLoaded, anyAdsLoadSucceeded:',
                anyAdsLoadSucceeded
              );
            },
      
            onAdLoaded: () => {
              console.log('onAdLoaded');
            },
      
            onAdClicked: () => {
              console.log('onAdClicked');
            },
      
            onAdImpression: (adSourceName) => {
              console.log('onAdImpression, adSourceName:', adSourceName);
            },
      
            onAdFailed: (adError) => {
              console.log('onAdFailed, adError:', adError);
            },
      
            // iOS only
            onAdPlayFailed: (adError) => {
              console.log('onAdPlayFailed, adError:', adError);
            },
      
            onAdClosed: () => {
             console.log('onAdClosed');
            },
      
            onAdReward: (currencyName, amount) => {
             this.callback(amount);
            },
      
            // iOS only
            onAdNotReward: () => {
              console.log('onAdNotReward');
            },
      
            // Android only
            onOneLayerLoaded: (adSourceName) => {
              console.log('onOneLayerLoaded, adSourceName:', adSourceName);
            },
      
            // Android only
            onOneLayerLoadFailed: (adSourceName, adError) => {
              console.log(
                'onOneLayerLoadFailed, adSourceName:',
                adSourceName,
                'adError:',
                adError
              );
            },
          });
    }
    showVideoAd(cb) {
        if(!cc.sys.isNative) return cc.error("you must run the application on Android or Ios");
        if (this.rewardedVideo && this.rewardedVideo.ready) {
            console.log('Show Ad');
            this.rewardedVideo.showAd('');
        } else {
            this.loadVideoAd();
        }
        this.callback = cb;
    }
    showBannerAd(position: bannerPos = "top") {
        this.banner.loadAd(position);
    }
    callback(callbackInfo: any) {
        
    }
  
}